// "pages/live/live",
import {
    HTTP
} from './utils/http.js'
let ajax = new HTTP()
App({
    onLaunch: function() {
        // 展示本地存储能力
        var logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                let code = res.code
                ajax.request({
                    method: "POST",
                    url: "weixin/getsessionkey",
                    data: {
                        code: code
                    },
                    success: (res) => {
                        this.globalData.memberid = res.memberid
                        this.globalData.openid = res.openid
                        wx.setStorageSync("memberid", res.memberid)
                        wx.setStorageSync("openid", res.openid)
                        if (this.globalData.userInfo === null) {
                            
                        } else {
                            let userInfo = this.globalData.userInfo
                            ajax.request({
                                method: "POST",
                                url: "member",
                                data: {
                                    id: this.globalData.memberid,
                                    nickname: userInfo.nickName,
                                    headimgurl: userInfo.avatarUrl,
                                    city: userInfo.city,
                                    country: userInfo.country,
                                    sex: userInfo.gender
                                },
                                success: (res) => {
                                    
                                }
                            })
                        }
                        if (this.memberidReadyCallback) {
                            this.memberidReadyCallback(res.memberid)
                        }
                        if (this.openidReadyCallback) {
                            this.openidReadyCallback(res.openid)
                        }
                    }
                })
            }
        })
        // 获取用户信息
        wx.getSetting({
            success: res => {
                // 已经授权&& this.globalData.userInfo != null
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            if (res.userInfo) {
                            
                            }
                            // 可以将 res 发送给后台解码出 unionId
                            this.globalData.userInfo = res.userInfo
                            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (this.userInfoReadyCallback) {
                                console.log("res.userInfo:" + res.userInfo)
                                this.userInfoReadyCallback(res.userInfo)
                            }
                        }
                    })
                } else {
                    // 未授权
                    wx.reLaunch({
                        url: '/pages/welcome/welcome',
                    })
                }
            }
        })
    },
    globalData: {
        openid: null,
        userInfo: null,
        memberid: null
    }
})
